home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / HippoDrawSrc1.1 / Hippo.subproj / Image.m < prev    next >
Encoding:
Text File  |  1992-04-25  |  3.4 KB  |  143 lines

  1. #import "Image.h"
  2. #import "GraphicView.h"
  3. #import <appkit/NXImage.h>
  4. #import <appkit/Panel.h>
  5. #import <dpsclient/wraps.h>
  6. #import <mach.h>
  7.  
  8. /* Optimally viewed in a wide window.  Make your window big enough so that this comment fits on one line without wrapping. */
  9.  
  10. @implementation Image : Graphic
  11. /*
  12.  * Image is a simple graphic which takes PostScript or
  13.  * TIFF images and draws them in a bounding box (it scales
  14.  * the image if the bounding box is changed).  It is
  15.  * implemented using the NXImage class.  Using NXImage
  16.  * here is especially nice since it images its PostScript
  17.  * in a separate context (thus, any errors that PostScript
  18.  * generates will not affect our main drawing context).
  19.  */
  20.  
  21. /* Set the class version */
  22.  
  23. + initialize
  24. {
  25.     [self setVersion:2];
  26.     return self;
  27. }
  28.  
  29. /* Creation methods */
  30.  
  31. - initFromStream:(NXStream *)stream allowAlpha:(BOOL)isAlphaOk
  32. /*
  33.  * Creates a new NXImage and sets it to be scalable and to retain
  34.  * its data (which means that when we archive it, it will actually
  35.  * write the TIFF or PostScript data into the stream).
  36.  */
  37. {
  38.     [super init];
  39.     if (image = [[NXImage allocFromZone:[self zone]] initFromStream:stream]) {
  40.     [image getSize:&originalSize];
  41.     [image setScalable:YES];
  42.     [image setDataRetained:YES];
  43.     alphaOk = isAlphaOk;
  44.     bounds.size = originalSize;
  45.     } else {
  46.     [self free];
  47.     self = nil;
  48.     }
  49.     return self;
  50. }
  51.  
  52. - free
  53. {
  54.     [image free];
  55.     return [super free];
  56. }
  57.  
  58. /* Methods overridden from superclass */
  59.  
  60. - (BOOL)isOpaque
  61. {
  62.     return alphaOk ? NO : YES;
  63. }
  64.  
  65. - (float)naturalAspectRatio
  66. {
  67.     if (!originalSize.height) return 0.0;
  68.     return originalSize.width / originalSize.height;
  69. }
  70.  
  71. - draw
  72. /*
  73.  * If we are resizing, we just draw a gray box.
  74.  * If not, then we simply see if our bounds have changed
  75.  * and update the NXImage object if they have.  Then,
  76.  * if we do not allow alpha (i.e. this is a TIFF image),
  77.  * we paint a white background square (we don't allow
  78.  * alpha in our TIFF images since it won't print and
  79.  * Draw is WYSIWYG).  Finally, we SOVER the image.
  80.  * If we are not keeping the cache around, we tell
  81.  * NXImage to toss its cached version of the image
  82.  * via the message recache.
  83.  */
  84. {
  85.     NXSize currentSize;
  86.  
  87.     if (bounds.size.width < 1.0 || bounds.size.height < 1.0) return self;
  88.  
  89.     if (DrawStatus == Resizing) {
  90.     PSsetgray(NX_DKGRAY);
  91.     PSsetlinewidth(0.0);
  92.     PSrectstroke(bounds.origin.x, bounds.origin.y,
  93.              bounds.size.width, bounds.size.height);
  94.     } else if (image) {
  95.     [image getSize:¤tSize];
  96.     if (currentSize.width != bounds.size.width || currentSize.height != bounds.size.height) [image setSize:&bounds.size];
  97.     if (!alphaOk) {
  98.         PSsetgray(NX_WHITE);
  99.         NXRectFill(&bounds);
  100.     }
  101.     [image composite:NX_SOVER toPoint:&bounds.origin];
  102.     if (dontCache && NXDrawingStatus == NX_DRAWING) [image recache];
  103.     }
  104.  
  105.     return self;
  106. }
  107.  
  108. - setCacheable:(BOOL)flag
  109. {
  110.     dontCache = flag ? NO : YES;
  111.     return self;
  112. }
  113.  
  114. - (BOOL)isCacheable
  115. {
  116.     return !dontCache;
  117. }
  118.  
  119. - write:(NXTypedStream *)stream
  120. /*
  121.  * All that is needed to archive the NXImage.
  122.  */
  123. {
  124.     [super write:stream];
  125.     NXWriteObject(stream, image);
  126.     NXWriteSize(stream, &originalSize);
  127.     NXWriteTypes(stream, "c", &alphaOk);
  128.     return self;
  129. }
  130.  
  131. - read:(NXTypedStream *)stream
  132. {
  133.     [super read:stream];
  134.     image = NXReadObject(stream);
  135.     NXReadSize(stream, &originalSize);
  136.     if (NXTypedStreamClassVersion(stream, "Image") > 1) {
  137.     NXReadTypes(stream, "c", &alphaOk);
  138.     }
  139.     return self;
  140. }
  141.  
  142. @end
  143.